import { Effect } from 'umi';
import { message } from 'antd';
import {saveDict,queryDictList} from "@/services/dict"

export interface DictType{
  ID: number
  CreatedAt: string
  UpdatedAt: string
  DeletedAt: string
  type:string
  desc:string
  label:string
  value:string
  sort:number
}
export interface DictState {
  dictlist?:DictType[];
}

export interface DictModelType {
  namespace: string;
  state: {};
  effects: {
    saveDict: Effect;
    init:Effect;
  };
  reducers: {
    saveState: Reducer<DictState>;
  };
}

const DictModel: DictModelType = {
  namespace: 'dict',

  state: {},

  effects: {
    *saveDict({ payload }, { call }) {
      yield call(saveDict, payload);
      message.success('提交成功');
    },
    *init({payload }, { call, put,select }) {
      const dictlist = yield select(state => state.dictlist)
      if(!dictlist){
        const res = yield call(queryDictList, payload);
        const list = res.data
        yield put({
          type: 'saveState',
          payload: {
            dictlist: Array.isArray(list) ? list : [],
          },
        });
      }
      
    },
  },
  reducers: {
    saveState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};

export default DictModel;
